// 导入数据库操作模块
const JSONParser = require('formidable/src/parsers/JSON')
const db = require('../../db/index')
const { getCurrentTime } = require('../../utils/user')
const serveImage = require('../../utils/serveImage')
var async = require('async')

// 获取轮播图数据
exports.getAllBanner = (req, res) => {
    // 1 代表未删除  0代表删除
    const sql = `select * from banner where b_delete=1`
    db.query(sql, (err, results) => {
        if (err) return res.operationFailed(err)
        return res.operationSuccess('success', '', '获取轮播图成功！', results)
    })
}

// 获取轮播图数据
exports.getBanner = (req, res) => {
    // 1 代表未删除  0代表删除
    const sql = `select * from banner where b_delete=1 and b_state=1`
    db.query(sql, (err, results) => {
        if (err) return res.operationFailed(err)
        return res.operationSuccess('success', '', '获取轮播图成功！', results)
    })
}

// 添加轮播图
exports.uploadBanner = (req, res) => {
    let { b_name, b_link } = req.body
    let file = req.file
    // 此时，图片已经保存至我们的服务端了
    let fileInfo = {}
    // 获取文件基本信息，封装好发送给前端
    fileInfo.name = b_name == '' ? file.filename : b_name
    fileInfo.path = `/api/out/serverImage/banner/${file.filename}`
    fileInfo.filename = file.filename
    fileInfo.link = b_link
    let time = getCurrentTime('date')
    const sql = `insert into banner(b_name, b_filename, b_path, b_time, b_link, b_state, b_delete) values('${ fileInfo.name }','${ fileInfo.filename }', '${ fileInfo.path }', '${ time }', '${ fileInfo.link }', 1, 1)`
    db.query(sql, (err, results) => {
        if (err) return res.operationSuccess('failure', 'ERR030', '添加轮播图失败！', err)
        return res.operationSuccess('success', '', '添加轮播图成功！', fileInfo)
    })
}

// 根据id删除轮播图（将轮播图的 b_state 更改为 0）
exports.deleteBannerById = (req, res) => {
    const { bid } = req.body 
    const sql = `update banner set b_delete=0 where bid in (${ bid })`
    db.query(sql, (err, results) => {
        if (err) return res.operationSuccess('failure', 'ERR031', '删除轮播图失败！', err)
        return res.operationSuccess('success', '', '删除轮播图成功！', [])
    })
}

// 根据名称/时间查找轮播图(模糊查询)
exports.searchBannerByNameOrTime = (req, res) => {
    const { searchType, searchText } = req.body
    // 判断用户是根据名称还是时间查询
    // N 为根据姓名查询， T 为根据时间查询
    let type = ''
    searchType == 'N' ? type = 'b_name' : type = 'b_time' 
    // 1 代表未删除  0代表删除
    const sql = `select * from banner where ${ type } like '%${ searchText }%' and b_delete=1`
    db.query(sql, (err, results) => {
        if (err) return res.operationFailed(err)
        return res.operationSuccess('success', '', '查找轮播图成功！', results)
    })
}

// 根据id更改轮播图信息
exports.updateBannerById = (req, res) => {
    const bannerInfo = req.body
    const file = req.file
    const slectSql = `select * from banner where bid=${ bannerInfo.bid }`
    db.query(slectSql, (err, selectRes) => {
        if (err) return res.operationFailed(err)
        if(selectRes.length == 0) return res.operationSuccess('failure', 'ERR036', '该轮播图不存在！', [])
        const b_name = bannerInfo.b_name ? bannerInfo.b_name : selectRes[0].b_name
        let b_path = ''
        let b_filename = ''
        let b_time = ''
        if(req.file) {
            // b_path = `http://localhost:3007/api/out/serverImage/${file.filename}`
            b_path = `/api/out/serverImage/banner/${file.filename}`
            b_filename = file.filename
            b_time = getCurrentTime('date')
        } else {
            b_path = bannerInfo.b_path ? bannerInfo.b_path : selectRes[0].b_path
            b_filename = bannerInfo.b_filename ? bannerInfo.b_filename : selectRes[0].b_filename
            b_time = selectRes[0].b_time
        }
        const b_link = bannerInfo.b_link ? bannerInfo.b_link : selectRes[0].b_link
        const b_state = bannerInfo.b_state ? bannerInfo.b_state : selectRes[0].b_state
        // 如果是更新轮播图的图片信息则传入 changeType='img', 更新添加时间
        const updateSql = `update banner set b_name='${b_name}', b_path='${b_path}', b_link='${b_link}', b_state='${b_state}', b_time='${b_time}', b_filename='${b_filename}' where bid=${bannerInfo.bid}`
        db.query(updateSql, (err, updateRes) => {
            if (err) return res.operationSuccess('failure', 'ERR032', '更新轮播图失败！', err)
            return res.operationSuccess('success', '', '更新轮播图成功！', [])
        })
    })
}

